//
//  MBDGroupSearchViewModel.swift
//  MasterBoy
//
//  Created by kyson on 2023/4/27.
//

import Foundation
import YYText
import SnapKit
import Alamofire
import SwiftyJSON
import Toast_Swift
import MJRefresh
import Foundation


class MBDGroupSearchTitleCellModel : MBDCommonCellModel {
    var title : String?
    
    override func cellHeight() -> CGFloat {
        return 36
    }
}

class MBDGroupSearchResultCellModel : MBDCommonCellModel {

    var group : MBDGroupDetailModel?
    
    override func cellHeight() -> CGFloat {
        return 85
    }
}

class MBDGroupSearchViewModel : NSObject {
    var cellModels : Array<Any>!
    
    override init() {
        //        self.cellModels = MBDGroupSearchViewModel.defaultCellModels()

    }
    
    static func defaultCellModels() ->Array<Any> {
        var cellModels = [Any]()
        
        let cellModel0 = MBDGroupSearchTitleCellModel()
        cellModel0.title = "群"
        cellModels.append(cellModel0)
        
        let cellModel1 = MBDGroupSearchResultCellModel()
        let group0 = MBDGroupDetailModel()
        group0.groupName = "kyson 的亿万俱乐部"
        group0.groupDesc = "kyson 的亿万俱乐部kyson 的亿万俱乐部kyson 的亿万俱乐部kyson 的亿万俱乐部kyson 的亿万俱乐部"
        cellModel1.group = group0
        cellModels.append(cellModel1)
        
        return cellModels
    }
    
    func doRequest(kw : String, completion : @escaping (_ error : Error?, _ result : Array<MBDGroupSearchResultCellModel>?,_ total : Int)->()) {
        
        MBDHttpClient.shared.doGetRequest("/group/listPage",parameters: ["keyword":kw ],encoding: URLEncoding.default).httpClientResponse { error, response in
            
            let rawDict = response?.rawDict!
            let rawRows = rawDict?["data"]
            let total = rawDict?["total"] as! Int
            if rawRows is Array<Any> {
                let rows = rawRows as! Array<Any>
                var multiRows = Array<MBDGroupSearchResultCellModel>()

                for row in rows {
                    let item = MBDGroupDetailModel(JSON(rawValue: row) ?? JSON(NSObject()))
                    let cellModel = MBDGroupSearchResultCellModel()
                    cellModel.group = item
                    multiRows.append(cellModel)
                }
                completion(error,multiRows,total)
            } else {
                completion(error,nil,0)
            }
        }
    }
}
